package com.gs.commentvl.dao;

import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Repository;

import com.gs.commentvl.model.Post;
import com.gs.commentvl.utils.RedisKeyUtil;

@Repository
public class PostDaoImpl extends GenericDao<Post> implements PostDao {

	@Autowired
	private RedisTemplate<String, Post> redisTemplate;

	@Override
	public void addNew(Post post) {
		String postKey = RedisKeyUtil.getKeyPost();
		redisTemplate.opsForZSet().add(postKey, post, post.getPostedTime());
	}

	@Override
	public Set<Post> getAll() {
		String postKey = RedisKeyUtil.getKeyPost();
		Set<Post> posts = redisTemplate.opsForZSet().range(postKey, 0, -1);
		return posts;
	}

	@Override
	public long getPostKey() {
		String postIdKey = RedisKeyUtil.getKeyPostId();
		long postKey = redisTemplate.opsForValue().increment(postIdKey, 1);
		return postKey;
	}

	@Override
	public Set<Post> getPostLimit(long offset, long count) {
		String postKey = RedisKeyUtil.getKeyPost();
		long currentTime = System.currentTimeMillis();
		Set<Post> posts = redisTemplate.opsForZSet().reverseRangeByScore(postKey, 0, currentTime, offset, count);
		return posts;
	}

	@Override
	public Set<Post> getNew() {
		//TODO Auto-generated method stub
		return null;
	}

	@Override
	public void addHash(String key, Map<String, String> Map) {
		// TODO Auto-generated method stub

	}

	@Override
	public Map<String, String> getHash(String key) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean existKey(String key) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public long getPostNum() {
		String postKey = RedisKeyUtil.getKeyPost();
		long currentTime = System.currentTimeMillis();
		long size = redisTemplate.opsForZSet().count(postKey, 0, currentTime);
		return size;
	}

}
